package com.db.protocol.tags;

import com.db.protocol.exception.IncorrectValueProtocolException;
import com.db.protocol.exception.NotParsingValueProtocolException;
import com.db.protocol.exception.ParseProtocolException;

public enum OrderType {
	
	MARKET(1),
	LIMIT(2);
	
	private final int type;
	
	public int value() {
		return type;
	}
	
	OrderType(int type) {
		this.type = type;
	}
	
	public static Tag tag() {
		return Tag.ORDER_TYPE;
	}

	public static OrderType parse(String message) throws ParseProtocolException {
		if (message == null) {
			throw new NotParsingValueProtocolException(tag());
		}
		try {
			int value = Integer.valueOf(message);
		
			for (OrderType type : values()) {
				if (type.value() == value) {
					return type;
				}
			}
		} catch (NumberFormatException exception) {
			throw new NotParsingValueProtocolException(tag());
		}
		throw new IncorrectValueProtocolException(tag());	
	}
	
}
